package com.mapforge.map.tools.tilewriting;

import java.awt.event.KeyEvent;

import com.mapforge.listener.RenderEvent;
import com.mapforge.selection.SelectionModifier;
import com.mapforge.selection.SelectionSquareSizeModifier;

public abstract class AbstractShapeTileWriterTool extends
		AbstractTileWriterTool {

	private boolean isShiftDown;
	private static final SelectionSquareSizeModifier squareModifier = new SelectionSquareSizeModifier();
	private final RenderEvent NULL_EVENT;

	public AbstractShapeTileWriterTool(String description) {
		super(description);
		this.isShiftDown = false;
		this.NULL_EVENT = new RenderEvent(this);
	}

	public boolean isShiftDown() {
		return this.isShiftDown;
	}

	public void keyPressed(KeyEvent e) {
		if (!this.isShiftDown && e.getKeyCode() == KeyEvent.VK_SHIFT) {
			this.isShiftDown = true;
			this.makeSelection();
			this.fireRenderListenerEvent(NULL_EVENT);
		}
	}

	@Override
	public SelectionModifier getSelectionModifier() {
		System.out.println("AbstractShapeTileWriterTool: getSelectionModifier");
		if (isShiftDown()) {
			return squareModifier;
		} else {
			return super.getSelectionModifier();
		}
	}

	public void keyReleased(KeyEvent e) {
		if (this.isShiftDown && e.getKeyCode() == KeyEvent.VK_SHIFT) {
			this.isShiftDown = false;
			this.makeSelection();
			this.fireRenderListenerEvent(NULL_EVENT);
		}
	}
}
